<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/6/29 0029
 * Time: 9:59
 */

namespace App\FreeBird\Controllers\Home;

use App\Base\Controllers\Controller;
use App\FreeBird\Contracts\InfoClassInterface;
use App\FreeBird\Contracts\InfoInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;


class InfoController extends Controller
{
    /**
     * 获取分类
     * @return InfoClassInterface
     */
    public function getInfoClassInterface()
    {
        return app()->make(InfoClassInterface::class);
    }

    /**
     * 获取信息接口
     * @return InfoInterface
     */
    public function getInfoInterface()
    {
        return app()->make(InfoInterface::class);
    }

    /**
     * 获取分类接口
     */
    public function getInfoClass(Request $request)
    {
        //获取是否置顶
        $where = function ($query) use ($request) {
            $recommended = $request->input("recommended", "0");
            if ($recommended == '') {
                $query->where("recommended", $recommended);
            }
            $name = $request->input("name", "");
            if (!empty($name)) {
                $query->where("name", "like", '%' . $name . '%');
            }
        };
        $infoClassModelList = $this->getInfoClassInterface()->getClassAll($where);
        $data = $infoClassModelList->toArray();
        $returnData = list_to_tree($data);
        return $returnData;
    }

    /**
     * 获取子分类
     * @param Request $request
     */
    public function getClassSub(Request $request)
    {
        return $this->getInfoClassInterface()->getClassSubAll($request->except("token"));
    }

    /**
     * 新增订单数据
     * @param Request $request
     * @return mixed
     */
    public function add(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'class' => 'required',
            'is_door' => 'required',
            'longitude' => 'required',
            'latitude' => 'required',
            'start_time' => 'required',
            'amount' => 'required',
            'num' => 'required',
            'unit' => 'required',
            'ordery_type' => 'required',
        ]);
        if ($validator->fails()) {
            //$error=$validator->errors()->getMessages();
            $this->throwValidatorException($validator);
        }

        return $this->getInfoInterface()->saveDataPay($request->except("token"));
    }


    /**
     * 根据订单id
     * @param Request $request
     */
    public function pay(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required'
        ]);
        if ($validator->fails()) {
            //$error=$validator->errors()->getMessages();
            $this->throwValidatorException($validator);
        }

        return $this->getInfoInterface()->getPayOrder($request->except("token"));
    }


    /**
     * 获取完成求邦信息列表
     * @param Request $request
     */
    public function filshList(Request $request)
    {
        $where = function ($query) {
            $query->where("order_status", 40);
        };
        return $this->getInfoInterface()->getOrderInfoListLimit($where);
    }

    /**
     * 求邦订单完成评论
     */
    public function comment(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'order_sn' => 'required',
            'score' => 'required',
            'content' => 'required',
        ]);
        if ($validator->fails()) {
            //$error=$validator->errors()->getMessages();
            $this->throwValidatorException($validator);
        }
        return $this->getInfoInterface()->setOrderInfoComment($request->except("token"));
    }

    /**
     * 获取我的订单列表
     * @param Request $request
     */
    public function getList(Request $request)
    {

        $where = function ($query) use ($request) {
            $orderType = $request->input("order_type");
            if (!empty($orderType)) {
                $query->where("order_type", $orderType);
            }
            $orderStatus = $request->input("order_status");
            if (!empty($orderStatus)) {
                $query->whereIn("order_status", explode(",", $orderStatus));
            }
            $query->where("uid", AUID);
        };
        return $this->getInfoInterface()->getList($where);
    }

    /**
     * 订单取消
     * @param Request $request
     * @return mixed
     */
    public function cancel(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required'
        ]);
        if ($validator->fails()) {
            //$error=$validator->errors()->getMessages();
            $this->throwValidatorException($validator);
        }
        $where = function ($query) use ($request) {
            $id = $request->input("id");
            if (!empty($id)) {
                $query->where("id", $id);
            }
            $query->where('uid', AUID);
        };
        return $this->getInfoInterface()->cancelOrder($where);
    }

    /**
     * 用户接单
     * @param Request $request
     * @return mixed
     */
    public function accept(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required'
        ]);
        if ($validator->fails()) {
            //$error=$validator->errors()->getMessages();
            $this->throwValidatorException($validator);
        }
        $where = function ($query) use ($request) {
            //$query->where("uid", AUID);
            $id = $request->input("id");
            $query->where("id", $id);
        };
        return $this->getInfoInterface()->acceptOrder($where);
    }

    /**
     * 用户－拒绝接单人
     * @param Request $request
     */
    public function refuse(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required'
        ]);
        if ($validator->fails()) {
            //$error=$validator->errors()->getMessages();
            $this->throwValidatorException($validator);
        }
        $where = function ($query) use ($request) {
            $query->where("uid", AUID);
            $id = $request->input("id");   //id不是订单id--接单人id
            $query->where("id", $id);
        };
        return $this->getInfoInterface()->refuseOrder($where);

    }

    /**
     * 用户-同意接单人
     * @param Request $request
     */
    public function agree(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required'
        ]);
        if ($validator->fails()) {
            //$error=$validator->errors()->getMessages();
            $this->throwValidatorException($validator);
        }
        $where = function ($query) use ($request) {
            $query->where("uid", AUID);
            $id = $request->input("id");   //id不是订单id--接单人id
            $query->where("id", $id);
        };
        return $this->getInfoInterface()->agreeOrder($where);
    }


    /**
     * 取消接单按钮---入驻会员
     */
    public function cancelAccept(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required'
        ]);
        if ($validator->fails()) {
            //$error=$validator->errors()->getMessages();
            $this->throwValidatorException($validator);
        }
        $where = function ($query) use ($request) {
            $query->where("uid", AUID);
            $id = $request->input("id");   //id不是订单id--接单人id
            $query->where("id", $id);
        };
        return $this->getInfoInterface()->cancelAccept($where);
    }

    /**
     * 退单按钮
     * @param Request $request
     */
    public function back(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required'
        ]);
        if ($validator->fails()) {
            //$error=$validator->errors()->getMessages();
            $this->throwValidatorException($validator);
        }
        $where = function ($query) use ($request) {
            $query->where("uid", AUID);
            $id = $request->input("id");   //id不是订单id--接单人id
            $query->where("id", $id);
        };
        return $this->getInfoInterface()->backOrder($where);
    }

    /**
     * 打赏按钮
     * @param Request $request
     */
    public function reward(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required',
            'mount' => 'required'
        ]);

        if ($validator->fails()) {
            //$error=$validator->errors()->getMessages();
            $this->throwValidatorException($validator);
        }
        $where = function ($query) use ($request) {
            $query->where("uid", AUID);
            $id = $request->input("id");   //id不是订单id--接单人id
            $query->where("id", $id);
        };
        $mount=$request->input("mount");
        return $this->getInfoInterface()->rewardOrder($where,$mount);
    }

    /**
     * 完成按钮---入驻会员
     * @param Request $request
     */
    public function completion(Request $request){

        $validator = Validator::make($request->all(), [
            'id' => 'required'
        ]);
        if ($validator->fails()) {
            //$error=$validator->errors()->getMessages();
            $this->throwValidatorException($validator);
        }
        $where = function ($query) use ($request) {
            $query->where("buid", AUID);
            $id = $request->input("id");   //id不是订单id--接单人id
            $query->where("id", $id);
        };
        return $this->getInfoInterface()->completionOrder($where);
    }

    /**
     * 放弃---入驻会员
     * @param Request $request
     */
    public function giveup(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required'
        ]);
        if ($validator->fails()) {
            //$error=$validator->errors()->getMessages();
            $this->throwValidatorException($validator);
        }
        $where = function ($query) use ($request) {
            $query->where("buid", AUID);
            $id = $request->input("id");   //id不是订单id--接单人id
            $query->where("id", $id);
        };
        return $this->getInfoInterface()->giveupOrder($where);
    }

    /**
     * 入驻会员关闭订单－即同意按钮
     * @param Request $request
     * @return mixed
     * @throws \App\Base\Exceptions\ApiException
     */
    public function rclose(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required'
        ]);
        if ($validator->fails()) {
            //$error=$validator->errors()->getMessages();
            $this->throwValidatorException($validator);
        }
        $where = function ($query) use ($request) {
            $query->where("buid", AUID);
            $id = $request->input("id");   //id不是订单id--接单人id
            $query->where("id", $id);
        };
        return $this->getInfoInterface()->closeOrder($where);
    }

    /**
     * 用户关闭订单－即同意按钮
     * @param Request $request
     * @return mixed
     * @throws \App\Base\Exceptions\ApiException
     */
    public function close(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required'
        ]);
        if ($validator->fails()) {
            //$error=$validator->errors()->getMessages();
            $this->throwValidatorException($validator);
        }
        $where = function ($query) use ($request) {
            $query->where("uid", AUID);
            $id = $request->input("id");   //id不是订单id--接单人id
            $query->where("id", $id);
        };
        return $this->getInfoInterface()->closeOrder($where);
    }

    /**
     * 这个同意要扣违约金
     * @param Request $request
     */
    public function giveupClose(Request $request){

    }


    /**
     * 订单投诉
     * @param Request $request
     */
    public function complaint(Request $request){
        $validator = Validator::make($request->all(), [
            'order_id' => 'required',
            'launch_type' => 'required',  //发起对象
            'content'=>'required'
        ]);
        if ($validator->fails()) {
            //$error=$validator->errors()->getMessages();
            $this->throwValidatorException($validator);
        }

        $where = function ($query) use ($request) {
            $launchType=$request->input("launch_type");
            if($launchType==2){
                $query->where("buid", AUID); //入驻会员发起投诉
            }else{
                $query->where("uid",AUID);  //普通会员发起投诉
            }
            $id = $request->input("order_id");   //id不是订单id--接单人id
            $query->where("id", $id);
        };
        return $this->getInfoInterface()->complaintOrder($where,$request->except("token"));
    }


    public function appeal(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required',         //投诉的id
            'appeal_content' => 'required',  //申诉的内容
        ]);
        if ($validator->fails()) {
            //$error=$validator->errors()->getMessages();
            $this->throwValidatorException($validator);
        }
        $where = function ($query) use ($request) {
            $id = $request->input("id");   //id不是订单id--接单人id
            $query->where("id", $id);
        };
        return $this->getInfoInterface()->appealOrder($where,$request->except("token"));
    }

    public function confirmationCompleted(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required'
        ]);
        if ($validator->fails()) {
            //$error=$validator->errors()->getMessages();
            $this->throwValidatorException($validator);
        }
        $where = function ($query) use ($request) {
            $query->where("uid", AUID);
            $id = $request->input("id");   //id不是订单id--接单人id
            $query->where("id", $id);
        };
        return $this->getInfoInterface()->confirmationCompletedOrder($where);
    }

    /**
     * 订单是否存在
     * @param Request $request
     */
    public function exitsOrder(Request $request){

        $validator = Validator::make($request->all(), [
            'id' => 'required'
        ]);
        if ($validator->fails()) {
            //$error=$validator->errors()->getMessages();
            $this->throwValidatorException($validator);
        }
        $where = function ($query) use ($request) {
            $query->where("uid", AUID);
            $query->where("order_status", "20");
            $query->where("order_type", 1);
           // $query->where("apply_status", 1);
        };
        return $this->getInfoInterface()->exitsOrder($where);
    }
    /**
     * 支付返回通知
     * @param Request $request
     */
    public function payNotify(Request $request)
    {
        $xml = file_get_contents('php://input');
        if($this->getInfoInterface()->payNotify($xml)){
            $arr=array();
            $arr["return_code"]="SUCCESS";
            $arr["return_msg"]="OK";
            echo  $this->arrayToXml($arr);  //支付成功返回
            exit;
        }else{
            $arr=array();
            $arr["return_code"]="Fail";
            $arr["return_msg"]="支付执行返回－求邦";
            echo  $this->arrayToXml($arr);  //支付成功返回
            exit;
        }
    }
    //数组转xml
    public function arrayToXml($arr) {
        $xml = "<xml>";
        foreach ($arr as $key => $val) {
            if (is_numeric($val)) {
                $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
            } else
                $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
        }
        $xml .= "</xml>";
        return $xml;
    }



    public function getInfoClassAll(){
        return $this->getInfoClassInterface()->getInfoClassAllMenu([]);
    }

}